package com.jinbiao.quasar.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.IdUtil;
import com.jinbiao.quasar.constant.RedisBucketKey;
import com.jinbiao.quasar.dto.CaptchaDto;
import com.jinbiao.quasar.util.AjaxJson;
import com.jinbiao.quasar.util.RedisUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;

@Controller
public class CaptchaController {
    @Mapping("/public/get-captcha")
    public AjaxJson getCaptcha(){
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(120, 40, 4, 4);
        captcha.setGenerator(randomGenerator);
        //图形验证码写出，可以写出到文件，也可以写出到流
        captcha.createCode();
        String captchaImage = "data:image/png;base64,"+captcha.getImageBase64();
        String captchaId = IdUtil.getSnowflakeNextIdStr();
        RedisUtil.getClient().getBucket().store(RedisBucketKey.CAPTCHA +captchaId,captcha.getCode());
        return AjaxJson.getSuccessData(new CaptchaDto(captchaId,captchaImage));
    }
}
